//!
/**
 ***********************************************************************************************************
 * <RBD3D9HardwareBufferManager Implementation>
 ***********************************************************************************************************
 * @file RBD3D9HardwareBufferManager.cpp
 ***********************************************************************************************************/

/***********************************************************************************************************
 * Precompiled Header
 ***********************************************************************************************************/
#include "RBD3D9RendererPCH.h"

/***********************************************************************************************************
 * Includes
 ***********************************************************************************************************/
#include "RBD3D9HardwareBufferManager.h"

#include "RBD3D9IndexBuffer.h"
#include "RBD3D9PixelBuffer.h"
#include "RBD3D9Renderer.h"
#include "RBD3D9Texture.h"
#include "RBD3D9Tools.h"
#include "RBD3D9VertexBuffer.h"
#include "RBD3D9VertexDeclaration.h"

/***********************************************************************************************************
 * Defines
 ***********************************************************************************************************/

/***********************************************************************************************************
 * Classes implementation
 ***********************************************************************************************************/

/**
 ***********************************************************************************************************
 * Constructor
 ***********************************************************************************************************/
RBD3D9HardwareBufferManager::RBD3D9HardwareBufferManager()
{
}

/**
 ***********************************************************************************************************
 * Destructor
 ***********************************************************************************************************/
RBD3D9HardwareBufferManager::~RBD3D9HardwareBufferManager()
{
}

/**************************************************************************************************************************************************************
 * 
 **************************************************************************************************************************************************************/
RBVertexBuffer* RBD3D9HardwareBufferManager::CreateVertexBuffer(uint32 uiUsage, uint32 uiVertexSize, uint32 uiVertexCount, const void* pInitData)
{
    RBVertexBuffer* pVertexBuffer = RBNew RBD3D9VertexBuffer(uiUsage, uiVertexSize, this, uiVertexCount, pInitData);
    return pVertexBuffer;
}

/**************************************************************************************************************************************************************
 * 
 **************************************************************************************************************************************************************/
RBIndexBuffer* RBD3D9HardwareBufferManager::CreateIndexBuffer(uint32 uiUsage, RBIndexBufferType::EType eIndexType, uint32 uiIndexCount, const void* pInitData)
{
    RBIndexBuffer* pIndexBuffer = RBNew RBD3D9IndexBuffer(uiUsage, eIndexType, this, uiIndexCount, pInitData);
    return pIndexBuffer;
}

/**************************************************************************************************************************************************************
 * 
 **************************************************************************************************************************************************************/
RBVertexDeclaration* RBD3D9HardwareBufferManager::CreateVertexDeclaration(RBVertexElement* pElements, uint32 uiElementCount)
{
    RBVertexDeclaration* pVertexDeclaration = RBNew RBD3D9VertexDeclaration(this, pElements, uiElementCount);
    return pVertexDeclaration;
}

/**************************************************************************************************************************************************************
 * 
 **************************************************************************************************************************************************************/
RBTexture* RBD3D9HardwareBufferManager::CreateTexture(const char* pcFilename)
{
    RBTexture* pTexture = RBNew RBD3D9Texture(pcFilename, this);
    return pTexture;

}

/***********************************************************************************************************
 * 
 ***********************************************************************************************************/
RBTexture* RBD3D9HardwareBufferManager::CreateTexture(uint32 uiUsage, uint32 uiWidth, uint32 uiHeight, RBPixelFormat eFormat, 
                                                      RBTextureType::EType eType, uint32 uiMips, uint32 uiDepth, uint32 eMemPool)
{
    RBTexture* pTexture = RBNew RBD3D9Texture(uiUsage, this, uiWidth, uiHeight, eFormat, eType, uiMips, uiDepth, eMemPool);
    return pTexture;
}

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
RBPixelBuffer* RBD3D9HardwareBufferManager::CreateRenderTarget(uint32 uiUsage, uint32 uiWidth, uint32 uiHeight, RBPixelFormat eFormat, 
                                                               uint32 uiMSAA)
{
    RBPixelBuffer* pBuffer = RBNew RBD3D9PixelBuffer(uiUsage, RBHardwareBuffer::MP_GPU, this, uiWidth, uiHeight, eFormat, uiMSAA, NULL);
    return pBuffer;
}

/***********************************************************************************************************
 *
 ***********************************************************************************************************/
void RBD3D9HardwareBufferManager::OnReset(bool bBeforeReset)
{
    for (std::list<RBHardwareBuffer*>::iterator iterBuffer = m_aAllocatedBuffers.begin(); iterBuffer != m_aAllocatedBuffers.end(); ++iterBuffer)
    {
        (*iterBuffer)->OnReset(bBeforeReset);
    }
}

/***********************************************************************************************************/
